package com.marketing.tenant.config;

import com.marketing.tenant.interceptor.AuthInterceptor;
import com.marketing.tenant.interceptor.TenantContextInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 * 配置拦截器、跨域等
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    @Lazy
    private AuthInterceptor authInterceptor;

    @Autowired
    private TenantContextInterceptor tenantContextInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 租户上下文拦截器（优先级更高）
        registry.addInterceptor(tenantContextInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/actuator/**",      // 排除健康检查
                        "/api/tenants/health",  // 排除健康检查接口
                        "/error",             // 排除错误页面
                        "/swagger-ui/**",     // 排除Swagger UI
                        "/v3/api-docs/**"     // 排除API文档
                )
                .order(1);  // 设置优先级
        
        // 权限拦截器
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/api/**")  // 拦截所有API路径
                .excludePathPatterns(
                        "/actuator/**",      // 排除健康检查
                        "/api/tenants/health",  // 排除健康检查接口
                        "/error",             // 排除错误页面
                        "/swagger-ui/**",     // 排除Swagger UI
                        "/v3/api-docs/**"     // 排除API文档
                )
                .order(2);  // 设置优先级，在租户上下文之后
    }
}